package com.neulogistics.contrib.warehouse.api.application.query;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neulogistics.contrib.warehouse.api.application.converter.CirculationOrderPOVMConverter;
import com.neulogistics.contrib.warehouse.api.application.converter.CirculationOrderQueryPOConverter;
import com.neulogistics.contrib.warehouse.api.application.viewmodel.CirculationOrderViewModel;
import com.neulogistics.contrib.warehouse.infrastructure.persistence.orm.po.CirculationOrderPO;
import com.neulogistics.contrib.warehouse.infrastructure.persistence.orm.service.ICirculationOrderPOService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CirculationOrderQueryService implements ICirculationOrderQueryService {
    @Resource
    ICirculationOrderPOService circulationOrderPOService;

    @Override
    public CirculationOrderViewModel getById(Long id) {
        CirculationOrderPO circulationOrderPO = circulationOrderPOService.getById(id);
        CirculationOrderViewModel circulationOrderViewModel = CirculationOrderPOVMConverter.POToVM(circulationOrderPO);
        return circulationOrderViewModel;
    }

    @Override
    public Page<CirculationOrderViewModel> page(CirculationOrderQuery circulationOrderQuery) {
        // 取出条件
        CirculationOrderPO circulationOrderPOQuery = CirculationOrderQueryPOConverter.queryToPO(circulationOrderQuery);
        long current = circulationOrderQuery.getPageNum();
        long pageSize = circulationOrderQuery.getPageSize();

        // 查询
        LambdaQueryWrapper<CirculationOrderPO> queryWrapper = new LambdaQueryWrapper<>(circulationOrderPOQuery);
        if(circulationOrderQuery.circulationStatus.length >= 1 && circulationOrderQuery.circulationStatus[0] != -1){
            ArrayList<Integer> status = new ArrayList<>(circulationOrderQuery.circulationStatus.length);
            for (int i : circulationOrderQuery.circulationStatus){
                status.add(i);
            }
            queryWrapper.in(CirculationOrderPO::getCirculationStatus, status);
        }
        Page<CirculationOrderPO> page = circulationOrderPOService.page(new Page<>(current, pageSize), queryWrapper);

        // 构造结果
        Page<CirculationOrderViewModel> viewModelPage = new Page<>(current, pageSize);
        List<CirculationOrderViewModel> viewModelList = page.getRecords().stream()
                .map(CirculationOrderPOVMConverter::POToVM).collect(Collectors.toList());
        viewModelPage.setRecords(viewModelList);
        viewModelPage.setTotal(page.getTotal());

        return viewModelPage;
    }

    @Override
    public List<CirculationOrderPO> getCirculationOrderByTransferId(Long transferOrderId) {
        // 根据transferOrderId，查询对应的出入库单
        LambdaQueryWrapper<CirculationOrderPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CirculationOrderPO::getTransferId, transferOrderId);
        List<CirculationOrderPO> circulationOrderPOList = circulationOrderPOService.list(queryWrapper);
        return circulationOrderPOList;
    }
}
